package com.lanyun.controller;

import cn.hutool.json.JSONUtil;
import cn.org.atool.fluent.mybatis.base.crud.IQuery;
import cn.org.atool.fluent.mybatis.metadata.JoinType;
import com.lanyun.common.R;
import com.lanyun.dao.intf.ScenicDao;
import com.lanyun.entity.vo.ImageReturn;
import com.lanyun.mapper.ScenicMapper;
import com.lanyun.wrapper.ImagelistQuery;
import com.lanyun.wrapper.ScenicQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description: TODO
 * @author: LanYun
 * @date: 2022/2/22 17:21
 * @Version: 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/test")
public class HelloController {
    @Autowired
    private ScenicDao scenicDao;
    @Autowired
    @Qualifier("scenicMapper")
    private ScenicMapper scenicMapper;

    //login
    @PostMapping("login")
    public R login() {
        return R.ok().data("token","admin");
    }
    //info
    @GetMapping("info")
    public R info() {
        return R.ok().data("roles","[admin]").data("name","admin").data("avatar","https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif");
    }

    @GetMapping("/{id}")
    public R selectById(@PathVariable("id") Long id){
        ScenicQuery leftQuery = new ScenicQuery("sc");
        ImagelistQuery rightQuery = new ImagelistQuery("il");
        rightQuery.select("id","image_url");
        leftQuery.select("name","image_id").where.imageId().eq(id);
        IQuery joinQuery = leftQuery.join(JoinType.LeftJoin, rightQuery)
                .on(l->l.where.imageId(),r->r.where.imageId()).endJoin()
                .build();
        List<Map<String, Object>> maps = scenicMapper.listMaps(joinQuery);
        List<ImageReturn> imageReturns = new ArrayList<>();
        for (Map<String, Object> map : maps) {
            imageReturns.add(JSONUtil.toBean(JSONUtil.toJsonStr(map), ImageReturn.class));
        }
        return R.ok().data("list",imageReturns);
    }

}
